package server.startup;

import server.Lifecycle;
import server.Wrapper;
import server.connector.http.HttpConnector;
import server.core.*;
import server.deploy.FilterDef;
import server.deploy.FilterMap;

import javax.servlet.FilterConfig;
import java.util.HashMap;

public class BootStrap {
    public static void main(String[] args) {
        HttpConnector connector = new HttpConnector();

        Wrapper wrapper1 = new StandardWrapper();
        wrapper1.setName("WrapperTest");
        wrapper1.setServletClass("WrapperTestServlet");
        wrapper1.addInitParameter("w1", "v1");
        wrapper1.addInitParameter("w2", "v2");

        Wrapper wrapper2 = new StandardWrapper();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");

        Wrapper wrapper3 = new StandardWrapper();
        wrapper3.setName("Primitive");
        wrapper3.setServletClass("PrimitiveServlet");

        StandardContext context = new StandardContext();
        context.addChild(wrapper1);
        context.addChild(wrapper2);
        context.addChild(wrapper3);
        //目录匹配
        context.addServletMapping("/wr/*", "WrapperTest");
        //后缀匹配
        context.addServletMapping("*.modern", "Modern");
        //默认匹配
        context.addServletMapping("/", "Primitive");
        //添加加载器
        context.setLoader(new SimpleLoader());
        //添加过滤器
        addFilter(context);
        //添加监听器
        context.addLifecycleListener(new SimpleContextConfig());

        connector.setContainer(context);
        try {
            connector.initialize();
            connector.start();
            ((Lifecycle)context).start();
            System.in.read();
            ((Lifecycle)context).stop();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    private static void addFilter(StandardContext context) {
        FilterDef filterDef1 = newFilterDef("F1", "Filter1");
        FilterDef filterDef2 = newFilterDef("F2", "Filter2");
        context.addFilterDef(filterDef1);
        context.addFilterDef(filterDef2);
        FilterMap filterMap1 = newFilterMap("F1", "Modern", "/wr/*");
        FilterMap filterMap2 = newFilterMap("F2", null, "/wr/f2");
        context.addFilterMap(filterMap1);
        context.addFilterMap(filterMap2);
        try {
            ApplicationFilterConfig filterConfig1 = new ApplicationFilterConfig(context, filterDef1);
            ApplicationFilterConfig filterConfig2 = new ApplicationFilterConfig(context, filterDef2);
            HashMap<String, FilterConfig> filterConfigs = context.getFilterConfigs();
            filterConfigs.put(filterConfig1.getFilterName(), filterConfig1);
            filterConfigs.put(filterConfig2.getFilterName(), filterConfig2);
        } catch (Throwable t) {
            t.printStackTrace();
        }

    }
    private static FilterDef newFilterDef(String filterName, String filterClass) {
        FilterDef filterDef = new FilterDef();
        filterDef.setFilterName(filterName);
        filterDef.setFilterClass(filterClass);
        filterDef.addInitParameter(filterName + ".p1", filterName + ".v1");
        filterDef.addInitParameter(filterName + ".p2", filterName + ".v2");
        return filterDef;
    }
    private static FilterMap newFilterMap(String filterName, String servletName, String urlPattern) {
        FilterMap filterMap = new FilterMap();
        filterMap.setFilterName(filterName);
        filterMap.setServletName(servletName);
        filterMap.setUrlPattern(urlPattern);
        return filterMap;
    }
    //    public void initial() {
//        FilterDef filterDef1 = new FilterDef();
//        filterDef1.setFilterClass("Filter1");
//        filterDef1.setFilterName("F1");
//        filterDef1.addInitParameter("f1", "k1");
//        filterDef1.addInitParameter("f2", "k2");
//
//        FilterDef filterDef2 = new FilterDef();
//        filterDef2.setFilterClass("Filter2");
//        filterDef2.setFilterName("F2");
//
//        try {
//            ApplicationFilterConfig filterConfig1 = new ApplicationFilterConfig(this, filterDef1);
//            ApplicationFilterConfig filterConfig2 = new ApplicationFilterConfig(this, filterDef2);
//            filterConfigs.put("F1", filterConfig1);
//            filterConfigs.put("F2", filterConfig2);
//        } catch (Throwable t) {
//            t.printStackTrace();
//        }
//    }
}
